<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class PropUserPack extends Base
{
    protected $table = 'prop_user_pack';
    protected $primaryKey = 'user_pack_id';

    protected $redis_glory_id = 'PROP:GLORY:ID';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 用户背包-创建/更新
     *
     * @param array $data 数据
     * @param string $type 操作类型 默认 insert=创建，update=更新，increment=累加，decrement=减少
     * @return bool
     * */
    public function updateOrInsertUserPack($data, $type = 'insert')
    {
        switch ($type) {
            case 'insert':
                $ret = DB::table($this->table)->insertGetId([
                    'user_id' => $data['user_id'],
                    'prop_id' => $data['prop_id'],
                    'num' => $data['num'],
                    'created_at' => time(),
                ]);
                break;
            case 'update':
                $ret = DB::table($this->table)->where([$this->primaryKey => $data[$this->primaryKey]])->update(['num' => $data['num']]);
                break;
            case 'increment':
                $ret = DB::table($this->table)->where([$this->primaryKey => $data[$this->primaryKey]])->increment('num', $data['num']);
                break;
            case 'decrement':
                $ret = DB::table($this->table)->where([$this->primaryKey => $data[$this->primaryKey]])->decrement('num', $data['num']);
                break;
            default:
                $ret = false;
                break;
        }

        return $ret;
    }

    /**
     * 查询用户背包
     *
     * @param int $userId 用户ID
     * @param boolean $filterGlory 是否过滤掉荣耀令
     * @return array
     * */
    public function getUserPack($userId ,$filterGlory = true)
    {
        // if($filterGlory){
        //     $glory_id = Redis::get($this->redis_glory_id) ?? 302;
        //     $db = DB::table($this->table)->where(['user_id' => $userId])
        //         ->where("num",">=",1)
        //         ->where('prop_id' ,'!=' ,$glory_id)
        //         ->orderBy('user_pack_id', 'asc');
        // }else{
            $db = DB::table($this->table)->where(['user_id' => $userId])->where("num",">=",1)->orderBy('user_pack_id', 'asc');
        // }
        $data = $db->get()->toArray();
        return self::objToArray($data);
    }

    /**
     * 检测用户背包道具是否存在
     *
     * @param int $userId 用户ID
     * @param int $propId 道具
     * @return array
     * */
    public function checkUserPack($userId, $propId)
    {
        $data = DB::table($this->table)->where(['user_id' => $userId, 'prop_id' => $propId])->first();
        return self::objToArray($data);
    }

    /**
     * 查询用户背包
     *
     * @param int $userId 用户ID
     * @param int $user_pack_id ID
     * @return array
     * */
    public function getUserPackById($userId, $user_pack_id)
    {
        $data = DB::table($this->table)->where(['user_pack_id' => $user_pack_id, 'user_id' => $userId])->first();
        return self::objToArray($data);
    }

}